package com.senriot.quickup.web.rest

import com.egzosn.pay.common.util.MatrixToImageWriter
import com.senriot.cloud.common.result.R
import com.senriot.quickup.domain.enums.OrderType
import com.senriot.quickup.service.PayService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.math.BigDecimal
import javax.imageio.ImageIO
import javax.servlet.http.HttpServletRequest

/**
 *
 * @property payService PayService
 * @property log (org.slf4j.Logger..org.slf4j.Logger?)
 * @constructor
 */

@RestController
@RequestMapping("/api/payment")
@Api(tags = ["支付"])
class PayResource(private val payService: PayService)
{

    private val log = LoggerFactory.getLogger(this.javaClass)

    /**
     * 获取订单二维码
     * @param deviceId String 设备ID
     * @param type OrderType 支付方式
     */
    @GetMapping("/order")
    @ApiOperation("获取订单二维码字符串")
    fun getQrPay(@RequestParam deviceId: String, @RequestParam type: OrderType): String?
    {
        return payService.createOrder(deviceId, type)
    }

    /**
     *
     * @param deviceId String
     * @param type OrderType
     * @return ByteArray?
     */
    @GetMapping("/order.jpg", produces = ["image/jpeg;charset=UTF-8"])
    @ApiOperation("获取订单二维码图片")
    fun toQrPay(@RequestParam deviceId: String, @RequestParam type: OrderType): ByteArray?
    {
        val str = payService.createOrder(deviceId, type)
        val baos = ByteArrayOutputStream()
        val buff = MatrixToImageWriter.writeInfoToJpgBuff(str)
        ImageIO.write(buff, "JPEG", baos)
        return baos.toByteArray();
    }

    /**
     * 微信支付回调
     * @param request HttpServletRequest
     * @return String
     * @throws IOException
     */
    @RequestMapping(value = ["/wx_notify_url"])
    @Throws(IOException::class)
    fun wxpayCallback(request: HttpServletRequest): String
    {
        return payService.wxPayService.payBack(request.parameterMap, request.inputStream).toMessage()
    }

    /**
     * 支付宝回调
     * @param request HttpServletRequest
     * @return String
     * @throws IOException
     */
    @RequestMapping(value = ["/ali_notify_url"])
    @Throws(IOException::class)
    fun alipayCallback(request: HttpServletRequest): String
    {
        //业务处理在对应的PayMessageHandler里面处理，在哪里设置PayMessageHandler，详情查看com.egzosn.pay.common.api.PayService.setPayMessageHandler()
        return payService.aliPayService.payBack(request.parameterMap, request.inputStream).toMessage()
    }


    @GetMapping("price")
    @ApiOperation("按设备ID获取单价")
    fun loadDevicePrice(@RequestParam @ApiParam("设备ID", required = true) deviceId: String): R<BigDecimal> =
            payService.loadPrice(deviceId)


    /**
     *  请求支付数据
     * @param deviceId String 设备ID
     * @param openId String alipay为支付宝
     * @param refId String
     * @param request HttpServletRequest
     * @return WxPaymentOrderResponse
     */
    @GetMapping("/{deviceId}")
    @ApiOperation("请求支付数据")
    fun payment(
            @PathVariable("deviceId") deviceId: String,
            @RequestParam("openId") openId: String,
            @RequestParam(value = "refId", defaultValue = "", required = false) refId: String,
            request: HttpServletRequest
    ): Any?
    {
        log.debug("payment request deviceId {} and openId {}", deviceId, openId)
        return payService.payment(deviceId, refId, request.remoteAddr, openId)
    }
}
